package com.buka.goods.controller;


import com.buka.entity.PageResult;
import com.buka.entity.Result;
import com.buka.goods.service.ProductService;
import com.buka.server.pojo.Product;
import com.buka.utils.BaseExceptionHandler;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/product")

/**
 * 商品管理
 */

public class ProductController {
    @Autowired
    private ProductService productService;

    /**
     * 查询全部
     * @return
     */
    @GetMapping("/findAll")
    public Result<List<Product>> findAll(){
        List<Product> all = productService.findAll();
        return new Result<>("查询成功",all);
    }

    /**
     * 条件查询
     * @param product
     * @return
     */
    @PostMapping("/findList")
    public Result<List<Product>> findList(@RequestBody Product product){
        List<Product> all = productService.findList(product);
        return new Result<>("查询成功",all);
    }

    /**
     * 查询全部分页
     * @return
     */
    @GetMapping("/findAllPage")
    public Result<PageResult<Product>> findAllPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size){
        Page<Product> allPage = productService.findAllPage(page, size);
        PageResult<Product> productPageResult = new PageResult<>();
        productPageResult.setRows(allPage.getResult());
        productPageResult.setTotal(allPage.getTotal());
        return new Result<>("查询成功",productPageResult);
    }

    /**
     * 条件查询分页
     * @param product
     * @return
     */
    @PostMapping("/findListPage")
    public Result<PageResult<Product>>  findListPage(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer size,@RequestBody Product product){
        Page<Product> listPage = productService.findListPage(page, size, product);
        PageResult<Product> productPageResult = new PageResult<>();
        productPageResult.setRows(listPage.getResult());
        productPageResult.setTotal(listPage.getTotal());
        return new Result<>("查询成功",productPageResult);
    }

    /**
     * 新增
     * @param product
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Product product){
        Integer add = productService.add(product);
        if (add != 1) {
            return new BaseExceptionHandler<>().error("新增失败");
        }
        return new Result<>("新增成功");
    }

    /**
     * 修改
     * @param product
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Product product){
        Integer update = productService.update(product);
        if (update != 1) {
            return new BaseExceptionHandler<>().error("修改失败");
        }
        return new Result<>("修改成功");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@RequestParam Long id){
        Integer delete = productService.delete(id);
        if (delete != 1) {
            return new BaseExceptionHandler<>().error("删除失败");
        }
        return new Result<>("删除成功");
    }


    /**
     * 上、下架
     * @param id
     * @return
     */
    @PostMapping("/status")
    public Result status(@RequestParam Long id,@RequestParam Integer status){
        Integer i = productService.status(id,status);
        if (i != 1) {
            return new BaseExceptionHandler<>().error("上下架失败");
        }
        if (status == 1){
            return new Result<>("下架成功");
        }
        return new Result<>("上架成功");
    }

}
